﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Main.Model.DATA;

namespace Main.Model.Entity
{
    public class Admin
    {
        #region Constructors

        public Admin()
        { }


        public Admin(
            //duyệt để lấy danh sách các khóa để truy vấn lấy 1 đối tượng cho contructer 1 có tham số khởi tạo
            Guid
            idGuid
              )
        {
            //gọi hàm lấy thông tin Private với tham số là danh sách các khóa như trên 
            GetAdmin(
                idGuid);
        }

        #endregion

        #region Private Properties

        private Guid idGuid = Guid.Empty;
        private string username = string.Empty;


        private string password = string.Empty;

       

        #endregion

        #region Public Properties

        public Guid IdGuid
        {
            get { return idGuid; }
            set { idGuid = value; }
        }
        public string Username
        {
            get { return username; }
            set { username = value; }
        }
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of Admin.
        /// </summary>
        /// <param name="idGuid"> idGuid </param>
        private void GetAdmin(
            Guid idGuid)
        {
            using (IDataReader reader = DBAdmin.GetOne(
                idGuid))
            {
                PopulateFromReader(reader);
            }

        }


        private void PopulateFromReader(IDataReader reader)
        {
            if (reader.Read())
            {
                this.idGuid = new Guid(reader["IdGuid"].ToString());
                this.username = reader["username"].ToString();
                this.password = reader["password"].ToString();

            }

        }

        /// <summary>
        /// Persists a new instance of Admin. Returns true on success.
        /// </summary>
        /// <returns></returns>
        public bool Create()
        {
            this.idGuid = Guid.NewGuid();

            int rowsAffected = DBAdmin.Create(
                this.idGuid,
                this.username,
                this.password);

            return (rowsAffected > 0);

        }


        /// <summary>
        /// Updates this instance of Admin. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update()
        {

            return DBAdmin.Update(
                this.idGuid,
                this.username,
                this.password);

        }





        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance of Admin. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save()
        {
            if (this.idGuid != Guid.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
        }




        #endregion

        #region Static Methods
        /// login 
        public static bool Login(string username , string pass)
        {
            if (DBAdmin.GetLogin(username, pass) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Deletes an instance of Admin. Returns true on success.
        /// </summary>
        /// <param name="idGuid"> idGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(
                Guid idGuid)
        {
            return DBAdmin.Delete(
                idGuid);
        }


        /// <summary>
        /// Gets a count of Admin. 
        /// </summary>
        public static int GetCount()
        {
            return DBAdmin.GetCount();
        }

        private static List<Admin> LoadListFromReader(IDataReader reader)
        {
            List<Admin> adminList = new List<Admin>();
            try
            {
                while (reader.Read())
                {
                    Admin admin = new Admin();
                    admin.idGuid = new Guid(reader["IdGuid"].ToString());
                    admin.username = reader["username"].ToString();
                    admin.password = reader["password"].ToString();
                    adminList.Add(admin);

                }
            }
            finally
            {
                reader.Close();
            }

            return adminList;

        }

        /// <summary>
        /// Gets an IList with all instances of Admin.
        /// </summary>
        public static List<Admin> GetAll()
        {
            IDataReader reader = DBAdmin.GetAll();
            return LoadListFromReader(reader);

        }

        /// <summary>
        /// Gets an IList with page of instances of Admin.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static List<Admin> GetPage(int pageNumber, int pageSize, out int totalPages)
        {
            totalPages = 1;
            IDataReader reader = DBAdmin.GetPage(pageNumber, pageSize, out totalPages);
            return LoadListFromReader(reader);
        }



        #endregion

    }

}
